package com.bigstar.lolkor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;
import java.util.StringTokenizer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.content.res.AssetManager.AssetInputStream;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class Quiz extends Activity implements OnClickListener {
	private static final char[] CHO = 
		/*ㄱ ㄲ ㄴ ㄷ ㄸ ㄹ ㅁ ㅂ ㅃ ㅅ ㅆ ㅇ ㅈ ㅉ ㅊ ㅋ ㅌ ㅍ ㅎ */
	{0x3131, 0x3132, 0x3134, 0x3137, 0x3138, 0x3139, 0x3141, 0x3142, 0x3143, 0x3145,
		0x3146, 0x3147, 0x3148, 0x3149, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e};
	public static ArrayList<Integer> N_QUIZ_INDEX = new ArrayList<Integer>();
	private Random r;

	private int stage;
	
	private int type;
	private int answer;
	private int[] quiz;
	private CountDownTimer timer;
	private int time;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		ChampionData.load(this);
		ItemData.load(this);
		Rune.load(this);
		r = new Random();
		quiz = new int[6];
		load();	
	}
	
    @Override
    protected void onDestroy() {
    	if(timer != null){
    		timer.cancel();
    	}
    	super.onResume();
    }
	
    @Override
    protected void onResume() {
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
    	super.onResume();
    }
    
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			new AlertDialog.Builder(Quiz.this)
			.setTitle("LOL백과사전")
			.setMessage("퀴즈를 종료 하시겠습니까?")
			.setIcon(R.drawable.icon)
			.setPositiveButton("예", new android.content.DialogInterface.OnClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which) {
					RankingDialog rank_dialog = new RankingDialog(Quiz.this, 1, 0, stage);
					rank_dialog.show();
				}
				
			})
			.setNegativeButton("아니오", new android.content.DialogInterface.OnClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
				
			})
			.show();
		}else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
			AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
			int volume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
			audio.setStreamVolume(AudioManager.STREAM_MUSIC, ++volume, AudioManager.FLAG_SHOW_UI);	
		}else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
			AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
			int volume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
			audio.setStreamVolume(AudioManager.STREAM_MUSIC, --volume, AudioManager.FLAG_SHOW_UI);
		}else if(keyCode == KeyEvent.KEYCODE_MENU){
			return false;
		}
		return true;
	}

	public void load() {
		time = 30000;
		stage = this.getIntent().getIntExtra("stage", 1);
		if(stage%5 == 0){
			this.setContentView(R.layout.quiz_1);
			int quiz_index = stage/5-1;
			if(quiz_index < N_QUIZ_INDEX.size()){
	    		AssetManager assetManager = getResources().getAssets();
	    		AssetInputStream ais;
	  	    	String quiz = "";
	  	    	int i = 0;
	    		try {
	    			ais = (AssetInputStream)assetManager.open("quiz.txt");
	    	        BufferedReader br = new BufferedReader(new InputStreamReader(ais));
	    	        String line;
	    	        while((line=br.readLine()) != null){
	    	        	if(i == N_QUIZ_INDEX.get(quiz_index)){
	    	        		quiz = line;
	    	        	}
	    	        	i++;
	    	        }
	    		} catch (IOException e) {
	    		} 
	            StringTokenizer st = new StringTokenizer(quiz, ";");
	    		String quiz_type = st.nextToken();
	       		String quiz_img = st.nextToken();
	       		String quiz_text = st.nextToken();
	       		String quiz_answer = st.nextToken();
				TextView tv = (TextView) findViewById(R.id.tv_quiz);
	       		if(quiz_type.equals("champ")){ 
	       			String temp = ChampionData.champions.get(r.nextInt(ChampionData.champions.size())).name_kor;	
	       			if(r.nextInt(3) == 0){
		       			temp = quiz_answer;	
	       			}
	       			quiz_text = quiz_text.replaceAll("%s", temp);
	       			if(quiz_answer.equals(temp)){
		       			tv.setTag("o");	
	       			}else{
		       			tv.setTag("x");
	       			}
					tv.setText(quiz_text);
	       		}else if(quiz_type.equals("ox")){
	    			tv.setText(quiz_text);
	       			tv.setTag(quiz_answer);	
	       		}else if(quiz_type.equals("system")){
	       			int temp = 0;
	       			if(r.nextInt(2) == 0){
	       				temp = Integer.parseInt(quiz_answer) + r.nextInt(3);
	       			}else{
	       				temp = Integer.parseInt(quiz_answer) - r.nextInt(3);
	       			}
	       			if(temp == Integer.parseInt(quiz_answer)){
		       			tv.setTag("o");	
	       			}else{
		       			tv.setTag("x");
	       			}
	       			quiz_text = quiz_text.replaceAll("%i", ""+temp);
	       			tv.setText(quiz_text);
	       		}else if(quiz_type.equals("short")){
	       			tv.setText(Html.fromHtml(quiz_text));
	    			tv.setTag(quiz_answer);
	        		ImageView iv = (ImageView) findViewById(R.id.iv_ox_o);
	        		iv.setVisibility(View.GONE);
	        		iv = (ImageView) findViewById(R.id.iv_ox_x);
	        		iv.setVisibility(View.GONE);
	       			LinearLayout layout = (LinearLayout) findViewById(R.id.lay_short_quiz);
	       			layout.setVisibility(View.VISIBLE);
	       			Button btn = (Button) findViewById(R.id.btn_quiz_ok);
	       			btn.setOnClickListener(this);
	       		}
	    		ImageView iv = (ImageView) findViewById(R.id.iv_quiz_img);
	       		if(quiz_img.equals("x")){
	       			iv.setVisibility(View.GONE);
	       		}else{
					int resid = getResources().getIdentifier(quiz_img, "drawable", getPackageName());	
	     			iv.setImageResource(resid);
	       		}
			}else{
				this.finish();
				Intent intent = new Intent(this, Quiz.class);
				intent.putExtra("stage", stage+1);
				this.startActivity(intent);
				return;
			}
    		ImageView iv = (ImageView) findViewById(R.id.iv_ox_o);
    		iv.setOnClickListener(this);
    		iv = (ImageView) findViewById(R.id.iv_ox_x);
    		iv.setOnClickListener(this);
		}else{
			type = r.nextInt(7);
			int answerCount = 4 + stage / 10;
			if(answerCount >= 6){
				answerCount = 6;	
			}
			if(type == 6){ //초성게임
				this.setContentView(R.layout.quiz_1);	
			}else{
				this.setContentView(R.layout.quiz_0);
			}
	    	if(type == 3){
	    		i:for (int i = 0; i < 6; i++) {
	    			quiz[i] = r.nextInt(ItemData.items.size());
    				if(ItemData.items.get(quiz[i]).text != null && ItemData.items.get(quiz[i]).text.indexOf("도미니언") != -1){
    					i--;
    					continue;
    				}
    				if(ItemData.items.get(quiz[i]).name_kor != null && ItemData.items.get(quiz[i]).name_kor.indexOf("증강") != -1){
    					i--;
    					continue;
    				}
    				if(ItemData.items.get(quiz[i]).name_kor != null && ItemData.items.get(quiz[i]).name_kor.indexOf("마공학") != -1){
    					i--;
    					continue;
    				}
    				if(ItemData.items.get(quiz[i]).text != null && ItemData.items.get(quiz[i]).text.indexOf("사라진 아이템") != -1){
    					i--;
    					continue;
    				}
	    			for(int j = 0; j < i; j++){
	    				if(quiz[i] == quiz[j]){
	    					i--;
	    					continue i;
	    				}
	    			}
	    			RelativeLayout rl = (RelativeLayout)findViewById(R.id.lay_quiz_0+(i*3));
	    			rl.setTag(quiz[i]);
	    			if(answerCount > i){
	    				rl.setOnClickListener(this);
	    				TextView tv = (TextView) findViewById(R.id.tv_quiz_0+(i*3));
	    				tv.setVisibility(View.INVISIBLE);
	    				ImageView iv = (ImageView) findViewById(R.id.iv_quiz_0+(i*3));
	    				iv.setImageResource(R.drawable.item_000+quiz[i]);
	    			}else{
	    				rl.setVisibility(View.INVISIBLE);
	    			}
	    		}
	    	}else if(type == 5){
	    		i:for (int i = 0; i < 6; i++) {
	    			quiz[i] = r.nextInt(ItemData.items.size());
    				if(ItemData.items.get(quiz[i]).text != null && ItemData.items.get(quiz[i]).text.indexOf("도미니언") != -1){
    					i--;
    					continue;
    				}
    				if(ItemData.items.get(quiz[i]).name_kor != null && ItemData.items.get(quiz[i]).name_kor.indexOf("증강") != -1){
    					i--;
    					continue;
    				}
    				if(ItemData.items.get(quiz[i]).name_kor != null && ItemData.items.get(quiz[i]).name_kor.indexOf("마공학") != -1){
    					i--;
    					continue;
    				}
    				if(ItemData.items.get(quiz[i]).text != null && ItemData.items.get(quiz[i]).text.indexOf("사라진 아이템") != -1){
    					i--;
    					continue;
    				}
	    			for(int j = 0; j < i; j++){
	    				if(quiz[i] == quiz[j]){
	    					i--;
	    					continue i;
	    				}
	    				int i_price = ItemData.items.get(quiz[i]).price;
	    				int j_price = ItemData.items.get(quiz[j]).price;
	    				if(i_price == j_price){
	    					i--;
	    					continue i;
	    				}
	    			}
	    			RelativeLayout rl = (RelativeLayout)findViewById(R.id.lay_quiz_0+(i*3));
	    			rl.setTag(quiz[i]);
	    			if(answerCount > i){
	    				rl.setOnClickListener(this);
	    				TextView tv = (TextView) findViewById(R.id.tv_quiz_0+(i*3));
	    				tv.setVisibility(View.INVISIBLE);
	    				ImageView iv = (ImageView) findViewById(R.id.iv_quiz_0+(i*3));
	    				iv.setImageResource(R.drawable.item_000+quiz[i]);
	    			}else{
	    				rl.setVisibility(View.INVISIBLE);
	    			}
	    		}
	    	}else if(type == 6){ //초성게임
	    		time = 30000;
				TextView tv = (TextView) findViewById(R.id.tv_quiz);
				if(r.nextInt(2) == 0){
					String quiz_answer = ChampionData.champions.get(r.nextInt(ChampionData.champions.size())).name_kor;
					String chosung = chosung(quiz_answer);
	       			tv.setText("["+chosung+"]라는 초성을 가진 챔피언을 쓰시오");
	    			tv.setTag(quiz_answer);	
		    		ImageView iv = (ImageView) findViewById(R.id.iv_quiz_img);
		    		iv.setVisibility(View.GONE);
				}else{
					int quiz_item = 0;
					String quiz_answer = "";
					do{
						quiz_item = r.nextInt(ItemData.items.size());
						quiz_answer = ItemData.items.get(quiz_item).name_kor;
					}while(quiz_answer.indexOf(":") != -1 && quiz_answer.indexOf(".") != -1);
					String chosung = chosung(quiz_answer);
	       			tv.setText("["+chosung+"]라는 초성을 가진 아이템을 쓰시오");
	    			tv.setTag(quiz_answer);	
		    		ImageView iv = (ImageView) findViewById(R.id.iv_quiz_img);
		    		iv.setImageResource(R.drawable.item_000+quiz_item);
				}
        		ImageView iv = (ImageView) findViewById(R.id.iv_ox_o);
        		iv.setVisibility(View.GONE);
        		iv = (ImageView) findViewById(R.id.iv_ox_x);
        		iv.setVisibility(View.GONE);
       			LinearLayout layout = (LinearLayout) findViewById(R.id.lay_short_quiz);
       			layout.setVisibility(View.VISIBLE);
       			Button btn = (Button) findViewById(R.id.btn_quiz_ok);
       			btn.setOnClickListener(this);
	    	}else{
	    		i:for (int i = 0; i < 6; i++) {
	    			quiz[i] = r.nextInt(ChampionData.champions.size());
	    			
	    			for(int j = 0; j < i; j++){
	    				if(quiz[i] == quiz[j]){
	    					i--;
	    					continue i;
	    				}
	    			}
	    			RelativeLayout rl = (RelativeLayout)findViewById(R.id.lay_quiz_0+(i*3));
	    			rl.setTag(quiz[i]);
	    			if(answerCount > i){
	    				rl.setOnClickListener(this);
	    				TextView tv = (TextView) findViewById(R.id.tv_quiz_0+(i*3));
	    				tv.setText(ChampionData.champions.get(quiz[i]).name_kor);
	    				ImageView iv = (ImageView) findViewById(R.id.iv_quiz_0+(i*3));
	    				String path = null;
	    				path = "champ_img/" + ChampionData.champions.get(quiz[i]).name_en + ".jpg";
	    				try {
	    					Drawable d = Drawable.createFromStream(getAssets().open(path), "src");
	    					d.setBounds(0, 0, 100, 100);
	    					iv.setBackgroundDrawable(d);
	    				} catch (IOException e) {
	    					// TODO Auto-generated catch block
	    					e.printStackTrace();
	    				}	
	    			}else{
	    				rl.setVisibility(View.INVISIBLE);
	    			}
	    		}	
	    	}
			answer = quiz[r.nextInt(answerCount)];
	    	if(type == 0){ //사운드 게임
	    		time = 15000;
	    		ImageView sound = (ImageView) findViewById(R.id.iv_quiz_sound);
	    		sound.setOnClickListener(this);	
	    	}else if(type == 1){	//별명맞추기
	    		time = 10000;
	    		ImageView sound = (ImageView) findViewById(R.id.iv_quiz_sound);
	    		sound.setVisibility(View.GONE);
				TextView tv = (TextView) findViewById(R.id.tv_quiz);
				tv.setText("다음 '"+ChampionData.champions.get(answer).nick_name+"'라는 별명을 가진 챔피언을 맞추시오.");
	    	}else if(type == 2){	//히스토리로 맞추기
	    		time = 30000;
	    		ImageView sound = (ImageView) findViewById(R.id.iv_quiz_sound);
	    		sound.setImageResource(R.drawable.history);
	    		sound.setOnClickListener(this);	
				TextView tv = (TextView) findViewById(R.id.tv_quiz);
				tv.setText("다음 배경을 보고 배경에 맞는 챔피언을 맞추시오.");
	    	}else if(type == 3){	//아이템 맞추기
	    		time = 15000;
	    		ImageView sound = (ImageView) findViewById(R.id.iv_quiz_sound);
	    		sound.setVisibility(View.GONE);
				TextView tv = (TextView) findViewById(R.id.tv_quiz);
				tv.setText("다음 '"+ItemData.items.get(answer).name_kor+"("+ItemData.items.get(answer).name_en+")'라는 아이템을 찾으시오.");
	    	}else if(type == 4){	//스킬로 챔프맞추기
	    		time = 10000;
	    		int r_skill = r.nextInt(4)+1;
	    		AssetManager assetManager = getResources().getAssets();
	    		AssetInputStream ais;
	    		int skill_index = 0;
	  	    	String skill_name[] = new String[5];
	    		try {
	    			ais = (AssetInputStream)assetManager.open("skill_text/"+ChampionData.champions.get(answer).name_en+".txt");
	    	        BufferedReader br = new BufferedReader(new InputStreamReader(ais));
	    	        String line;
	    	        while((line=br.readLine()) != null){
	    	        	if(skill_index < 5){
	    	        		skill_name[skill_index] = line;
	    	        	}else{
	    	        	}
	    	        	skill_index++;
	    	        }
	    		} catch (IOException e1) {
	    			// TODO Auto-generated catch block
	    			e1.printStackTrace();
	    		} 
	    		
	    		
	    		ImageView sound = (ImageView) findViewById(R.id.iv_quiz_sound);
	    		String path = null;
		        path = "skill_img/"+ChampionData.champions.get(answer).name_en+"_"+r_skill+".jpg";
				try {
					Drawable d = Drawable.createFromStream(getAssets().open(path), "src");
					d.setBounds(0, 0, 100, 100);
					sound.setImageDrawable(d);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
				TextView tv = (TextView) findViewById(R.id.tv_quiz);
				tv.setText("다음 '"+skill_name[r_skill]+"'스킬을 쓰는 챔피언을 찾으시오.");
	    	}else if(type == 5){	//아이템 가격 맞추기
	    		time = 20000;
	    		ImageView sound = (ImageView) findViewById(R.id.iv_quiz_sound);
	    		sound.setVisibility(View.GONE);
				TextView tv = (TextView) findViewById(R.id.tv_quiz);
				tv.setText("다음 '"+ItemData.items.get(answer).price+"'골드인 아이템을 찾으시오.");
	    	}
		}
		timer = new CountDownTimer(time, 50) {
            public void onTick(long millisUntilFinished) {   
				int sec = (int) (millisUntilFinished / 1000);
				int mil_sec = (int) ((millisUntilFinished % 1000)/100);
            	TextView tv_stage = (TextView) findViewById(R.id.tv_quiz_stage);
            	tv_stage.setText("문제 "+stage +"\r\n남은 시간 : "+sec+"."+mil_sec+"초");
            } 
       
            public void onFinish() {
				RankingDialog rank_dialog = new RankingDialog(Quiz.this, 1, 0, stage);
				rank_dialog.show();
            }   
        }.start(); 
        AdView adview = (AdView)findViewById(R.id.adView);
        AdRequest re = new AdRequest(); 
        adview.loadAd(re);
	}

	@Override
	public void onClick(View v) {
		if(stage%5 == 0){
			TextView tv = (TextView) findViewById(R.id.tv_quiz);
			if(v.getId() == R.id.iv_ox_o){
				if(((String)tv.getTag()).equals("o")){
					this.finish();
					Intent intent = new Intent(this, Quiz.class);
					intent.putExtra("stage", stage+1);
					this.startActivity(intent);
				}else{
					RankingDialog rank_dialog = new RankingDialog(Quiz.this, 1, 0, stage);
					rank_dialog.show();
				}
			}else if(v.getId() == R.id.iv_ox_x){
				if(((String)tv.getTag()).equals("x")){
					this.finish();
					Intent intent = new Intent(this, Quiz.class);
					intent.putExtra("stage", stage+1);
					this.startActivity(intent);
				}else{
					RankingDialog rank_dialog = new RankingDialog(Quiz.this, 1, 0, stage);
					rank_dialog.show();
				}
			}else if(v.getId() == R.id.btn_quiz_ok){
				EditText et = (EditText)findViewById(R.id.et_short_quiz);
				if(et.getText().toString().length() <= 0){
					new AlertDialog.Builder(this)
					.setTitle("정답을 써주세요.")
					.setMessage("정답을 써주세요.")
					.setNegativeButton("닫기", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
						}
					})
					.create().show();
					return;
				}
				String temp = tv.getTag().toString().replaceAll(" ", "");
				if(temp.equals(et.getText().toString().replaceAll(" ", ""))){
					this.finish();
					Intent intent = new Intent(this, Quiz.class);
					intent.putExtra("stage", stage+1);
					this.startActivity(intent);
				}else{
					RankingDialog rank_dialog = new RankingDialog(Quiz.this, 1, 0, stage);
					rank_dialog.show();
				}
			}
		}else{
			if(type == 6){
				TextView tv = (TextView) findViewById(R.id.tv_quiz);
				EditText et = (EditText)findViewById(R.id.et_short_quiz);
				if(et.getText().toString().length() <= 0){
					new AlertDialog.Builder(this)
					.setTitle("정답을 써주세요.")
					.setMessage("정답을 써주세요.")
					.setNegativeButton("닫기", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
						}
					})
					.create().show();
					return;
				}
				String temp = tv.getTag().toString().replaceAll(" ", "");
				if(temp.equals(et.getText().toString().replaceAll(" ", ""))){
					this.finish();
					Intent intent = new Intent(this, Quiz.class);
					intent.putExtra("stage", stage+1);
					this.startActivity(intent);
				}else{
					RankingDialog rank_dialog = new RankingDialog(Quiz.this, 1, 0, stage);
					rank_dialog.show();
				}
				return;
			}
			if(v.getId() == R.id.iv_quiz_sound){
		    	if(type == 0){ //사운드 게임
		    		MediaPlayer fileOfMusic = null;
		            AssetFileDescriptor afd = null;
		    		try {
		    			afd = getAssets().openFd("sound/"+ChampionData.champions.get(answer).name_en+".mp3");
		    			if(afd != null){
		    		        fileOfMusic = new MediaPlayer();
		    		        fileOfMusic.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
		    		        fileOfMusic.prepare();
		    		        afd.close();	
		    			}
		    		} catch (IOException e) {
		    			e.printStackTrace();
		    		}finally{
		    			try {
		    				if(afd != null)
		    					afd.close();
		    			} catch (IOException e) {
		    				// TODO Auto-generated catch block
		    				e.printStackTrace();
		    			}
		    		}
		        	
		        	if(fileOfMusic != null)
		        		fileOfMusic.start();	
		    	}else if(type == 2){	//히스토리로 맞추기
		    		String his = null;
					try {
						his = loadHistory();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
					new AlertDialog.Builder(this)
					.setTitle("챔피언 배경")
					.setMessage(his)
					.setNegativeButton("닫기", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
						}
					})
					.create().show();
		    	}
			}else{
				if((Integer)v.getTag() == answer){
					this.finish();
					Intent intent = new Intent(this, Quiz.class);
					intent.putExtra("stage", stage+1);
					this.startActivity(intent);
				}else{
					RankingDialog rank_dialog = new RankingDialog(Quiz.this, 1, 0, stage);
					rank_dialog.show();
				}
			}
		}
	}
	
	public String chosung(String chmapName) {
		String chosung = "";
		int rnd = -1;
		if(chmapName.length() > 2){
			do{
				rnd = r.nextInt(chmapName.length());
			}while(chmapName.charAt(rnd) == ' ');
		}else if(chmapName.length() == 2){
			rnd = 0;
		}
		for(int i = 0 ; i < chmapName.length();i++)
		{
			char test = chmapName.charAt(i);
			if(rnd == i){
				chosung += test;
			}else if(test >= 0xAC00){
				char uniVal = (char) (test - 0xAC00);
				
				char cho = (char) (((uniVal - (uniVal % 28))/28)/21);
				chosung += CHO[cho];
			}
			if(test == ' '){
				chosung += " ";
			}
		}
		
		
		
		return chosung;
	}

	
	public String loadHistory() throws IOException {
		AssetManager assetManager = this.getResources().getAssets();
		AssetInputStream ais = (AssetInputStream)assetManager.open("history/"+ChampionData.champions.get(answer).name_en+".txt"); 
        BufferedReader br = new BufferedReader(new InputStreamReader(ais));
        String line;
        StringBuilder data = new StringBuilder();
        while((line=br.readLine()) != null){
        	data.append(line);
        	data.append("\r\n");
        }
        
        ais.close();
        br.close();
        return data.toString().replaceAll(ChampionData.champions.get(answer).name_kor, "[X]");
	}
}
